package com.xtkj.superclub_user.controller;

import com.xtkj.commons.JsonParse;
import com.xtkj.commons.JsonResult;
import com.xtkj.commons.SnowflakeIdWorker;
import com.xtkj.commons.TokenUtil;
import com.xtkj.superclub_entity.entity.user.User;
import com.xtkj.superclub_user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lizheqi
 * @create 2019-12-23-20:06
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    /**
     * 用户登录
     * @param user
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public JsonResult checkLogin (@RequestBody User user){
        User dbuser = userService.checkLogin(user);
        try {
            if(dbuser!=null){
                if(dbuser.getIsLimit()==1){
                    return JsonResult.success().setMsg("你已经被限制登录").setCode(303);
                }
                if(dbuser.getPassword().equals(user.getPassword())){
                    String token = TokenUtil.sign(dbuser.getUserId(), user.getPassword());


                    return JsonResult.success().setData(dbuser).setMsg(token).setCode(200);
                }
                return JsonResult.error().setCode(402).setMsg("密码错误");
            }else {
                return JsonResult.error().setCode(404).setMsg("用户不存在");
            }
        }catch (Exception e){
            return JsonResult.error().setCode(400).setMsg(e.getMessage());
        }
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public JsonResult userRegister(@RequestBody User user){
        //每次注册通过雪花生成用户id
       try {
           String userId = snowflakeIdWorker.nextId()+"";
           System.out.println(user+"1");
           user.setUserId(userId);
           userService.userRegister(user);
           return JsonResult.success().setMsg("注册成功").setCode(200);
       }catch (Exception e){
           e.printStackTrace();
           return JsonResult.error().setCode(400).setMsg("注册失败");
       }

    }

    /**
     * 用户更新
     * @param user
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public JsonResult updateUser(@RequestBody User user){
        try {
            userService.updateUser(user);
            return JsonResult.success().setMsg("更新成功").setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error().setCode(400).setMsg("更新失败");
        }
    }

    /**
     * 获取的是登录的用户信息
     * @return
     */
    @RequestMapping(value = "/getUser",method = RequestMethod.GET)
    public JsonResult getUser(@RequestBody User user){
        try {
            User dbUser = userService.getUserById(user.getUserId());
            return JsonResult.success().setMsg("获取用户成功").setCode(200).setData(dbUser.toString());
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error().setCode(400).setMsg("获取用户失败");
        }
    }


    @RequestMapping(value = "/getUserByToken",method = RequestMethod.POST)
    public JsonResult getUserByToken(@RequestBody User user,@RequestParam("token") String token){
        try {
            User dbUser = userService.getUserById(user.getUserId());
            return JsonResult.success().setMsg("获取用户成功").setCode(200).setData(dbUser.toString());
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error().setCode(400).setMsg("获取用户失败");
        }
    }


    /**
     * 根据传递过来的用户的电话来判断数据库里面是否有该用户
     * @return
     */
    @RequestMapping(value = "/invite",method = RequestMethod.GET)
    public JsonResult inviteUser(@RequestBody User user){
        try {
            User dbUser = userService.getUserByTel(user.getTelephone());
            return JsonResult.success().setMsg("获取用户成功").setCode(200).setData(dbUser);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error().setCode(400).setMsg("获取用户失败");
        }
    }

    /**
     * 根据id获取用户信息
     * @return
     */
    @RequestMapping(value = "/getUserById",method = RequestMethod.GET)
    public String getUser(@RequestParam String likeUserId){
        try {
            User dbUser = userService.getUserById(likeUserId);
            String userstr = JsonParse.obj2String(dbUser);
            return userstr;
        }catch (Exception e){
            e.printStackTrace();
            return "错误";
        }
    }


}
